import { http } from '@/request/http'
import type { LabelVO } from '@/request/api/label'
import type { ClassifyEnum } from '@/enum'
import type { MakeOptional } from '@/type'

type BaseClassify = {
  id: number
  // 类别名
  name: string
  // 简介
  introduction: string
  // 优先级
  priority: number
  // 状态
  status: boolean
}

export type ClassifyDTO = MakeOptional<BaseClassify>

export type ClassifyVO = BaseClassify & {
  // 类型
  type: string
}

export type ClassifyLabelVO = ClassifyVO & {
  labels: LabelVO[]
}

export const initClassifyDTO = (): ClassifyDTO => {
  return {
    id: undefined,
    name: undefined,
    introduction: undefined,
    priority: undefined,
    status: undefined
  }
}

export const initClassifyVO = (): ClassifyVO => {
  return {
    id: 0,
    name: '',
    introduction: '',
    type: '',
    priority: 0,
    status: true
  }
}
export const classifyVO2DTO = (vo: ClassifyVO): ClassifyDTO => {
  return {
    id: vo.id,
    name: vo.name,
    introduction: vo.introduction,
    priority: vo.priority,
    status: vo.status
  }
}

export const getClassifyApi = (id: number) => {
  return http<ClassifyVO>({
    url: `/classify/${id}`,
    method: 'get'
  })
}

// 查询详细
export const getClassifyListApi = () => {
  return http<ClassifyVO[]>({
    url: `/admin/classify/list`,
    method: 'get'
  })
}

export const getClassifyLabelsTreeApi = (type: ClassifyEnum) => {
  return http<ClassifyLabelVO[]>({
    url: `/classify/label/tree`,
    method: 'get',
    params: {
      type: type
    }
  })
}

// 新增
export function addClassifyApi(data: ClassifyDTO) {
  return http<boolean>({
    url: '/admin/classify/insert',
    method: 'POST',
    data: data
  })
}

// 修改
export function updateClassifyApi(data: ClassifyDTO) {
  return http<boolean>({
    url: '/admin/classify/update',
    method: 'PUT',
    data: data
  })
}

// 删除
export function delClassifyApi(ids: number[]) {
  return http<boolean>({
    url: '/admin/classify/delete',
    method: 'DELETE',
    data: ids
  })
}
